/**
 * Created by yuweimei on 2018/3/27.
 */
app.controller('search', function($scope, $http) {
	$('.top_four_tips ul li').click(function() {
		console.log(1);
		$(this).siblings(".click_div").removeClass("click_div");
		$(this).addClass("click_div");
		$('.content_div').find('.display').removeClass("display").addClass("hidden");
		$('.content_div').find('.tab').eq($(this).index()).removeClass("hidden").addClass("display");
	});
	var token = localStorage.getItem('token');
	$scope.keyWord = sessionStorage.getItem('keyword');
	$scope.dynamicsList = [];
	$scope.dynamicsList1 = [];
	$scope.type = 0;
	$scope.searchTour = function() {
		if($scope.type == 0) {
			$scope.dynamicsList = [];
		} else {
			$scope.dynamicsList1 = [];
		}
		$http({
			method: 'get',
			url: app.netPath + '/dynamic/keyword',
			headers: {
				'Authorization': token
			},
			params: {
				page: 1,
				keyword: $scope.keyWord,
				type: $scope.type
			}
		}).then(function(res) {
			console.log(res.data);
			if(!res.data.error) {
				for(var i = 0; i < res.data.data.length; i++) {
					if(res.data.data[i].user.userAvatarAddress) {
						res.data.data[i].user.userAvatarAddress = app.netPath + '/resources/img/' + res.data.data[i].user.userAvatarAddress;
						app.buildImageUrl(res.data.data[i].user, 'userAvatarAddress', function() {
							$scope.$digest();
						});
					}
					//放置动态图片
					if(res.data.data[i].tourdynaImg) {
						//得到每条动态的图片数组
						var dynamicImgs = app.getDynamicImg(res.data.data[i].tourdynaImg);
						for(var j = 0; j < dynamicImgs.length; j++) {
							dynamicImgs[j] = app.netPath + '/resources/taskImg/' + dynamicImgs[j];
							app.buildImageUrl(dynamicImgs, j, function() {
								$scope.$digest();
							});
						}
						res.data.data[i].tourdynaImg = dynamicImgs;
					}
					if($scope.tourIdLikeList.length != 0) {
						for(var a = 0; a < $scope.tourIdLikeList.length; a++) {
							if(res.data.data[i].tourdynaId == $scope.tourIdLikeList[a]) {
								res.data.data[i].isLike = 1;
							} else {}
						}
					}
					var lat = res.data.data[i].positionX;
					var lng = res.data.data[i].positionY;
					res.data.data[i].distance = app.getGreatCircleDistance(lat, lng);
					if(res.data.data[i].distance >= 1000) {
						res.data.data[i].distance = (app.getGreatCircleDistance(lat, lng) / 1000).toFixed(2) + "千米";
					} else {
						res.data.data[i].distance = app.getGreatCircleDistance(lat, lng) + "米";
					}
					if($scope.type == 0) {
						$scope.dynamicsList.push(res.data.data[i]);
					} else {
						$scope.dynamicsList1.push(res.data.data[i]);
					}
				}
				console.log($scope.dynamicsList);

			} else {

			}

		})
	};
	//当前用户点赞动态id
	$scope.tourIdLikeList = [];
	//用户点赞记录
	$scope.showLikeList = function() {
		$scope.tourIdLikeList = [];
		if(!token) {
			$scope.searchTour();
		} else {
			//点赞动态列表id
			$http({
				method: 'get',
				url: app.netPath + '/dynamic/details/show/likes',
				headers: {
					'Authorization': token
				}
			}).then(function(res) {
				if(!res.data.error) {
					for(var i = 0; i < res.data.data.length; i++) {
						$scope.tourIdLikeList.push(res.data.data[i].likedynaId);
					}
				}
				$scope.searchTour();
			});
		}

	};
	//点赞
	$scope.addLike = function(tourdynaId, index) {
		if(!token) {
			//判断有无证书
			$.toast("请先登录");
			location.href = '#login';
			return;
		} else {
			$http({
				method: 'get',
				url: app.netPath + '/dynamic/details/likeAdd',
				headers: {
					'Authorization': token
				},
				params: {
					tourdynaId: tourdynaId
				}
			}).then(function(res) {
				if(!res.data.error) {
					if($scope.type == 0) {
						$scope.dynamicsList[index].isLike = 1;
						$scope.dynamicsList[index].tourdynaLikeNum = $scope.dynamicsList[index].tourdynaLikeNum + 1;
					}
					if($scope.type == 1) {
						$scope.dynamicsList1[index].isLike = 1;
						$scope.dynamicsList1[index].tourdynaLikeNum = $scope.dynamicsList1[index].tourdynaLikeNum + 1;
					}
				} else {
					// $.toast(res.data.error + ',' + res.data.msg);

				}
			});
		}
	};
	//取消点赞
	$scope.reduceLike = function(tourdynaId, index) {
		if(!token) {
			//判断有无证书
			$.toast("请先登录");
			// location.href = '#login';
			return;
		} else {
			$http({
				method: 'get',
				url: app.netPath + '/dynamic/details/likeReduce',
				headers: {
					'Authorization': token
				},
				params: {
					tourdynaId: tourdynaId
				}
			}).then(function(res) {
				if(!res.data.error) {
					if($scope.type == 0) {
						$scope.dynamicsList[index].isLike = 0;
						$scope.dynamicsList[index].tourdynaLikeNum = $scope.dynamicsList[index].tourdynaLikeNum - 1;
					}
					if($scope.type == 1) {
						$scope.dynamicsList1[index].isLike = 0;
						$scope.dynamicsList1[index].tourdynaLikeNum = $scope.dynamicsList1[index].tourdynaLikeNum - 1;
					}
				} else {
					$.toast(res.data.error + ',' + res.data.msg);
				}
			});
		}
	};
	$scope.getType = function(i) {
		$scope.type = i;
		$scope.showLikeList();
	};
	//跳转otherPerson详情页
	$scope.GoToOtherUser = function(userId) {
		sessionStorage.setItem('OtherUserId', userId);
	};
	$scope.GoToReview = function(tourdynaId) {
		sessionStorage.setItem('tourdynaInfoId', tourdynaId);
	};
});